package com.centaline.androidsalesblog.activities.sale;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseSaleListActivity;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.model.businissobject.SchoolEstateInfoBo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.rx.database.SearchDataFunc1;
import com.centaline.androidsalesblog.widgets.dropdown.MoreDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SalePriceDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SingleDrop;

import java.util.ArrayList;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func7;
import rx.schedulers.Schedulers;

/**
 * 学校对应二手房源
 * Created by guilin on 16/3/1.
 */
public class SchoolToSaleListActivity extends BaseSaleListActivity {

    public static final String SchoolToEsfList = "SchoolToEsfList";

    private SingleDrop singleDrop;
    private SalePriceDrop salePriceDrop;
    private SingleDrop postRoomDrop;
    private MoreDrop moreDrop;

    private ArrayList<SchoolEstateInfoBo> schoolEstateInfoBos = new ArrayList<>();

    @Override
    protected void initViews() {
        super.initViews();
        setUniversalToolbar(getIntent().getStringExtra(ExtraConstant.SchoolShortName));
        postListApi.setSchoolId(getIntent().getStringExtra(ExtraConstant.SchoolId));
    }

    @Override
    protected void initDrop() {
        dropDownView.initTabs(R.array.schoolToSaleList);
        singleDrop = new SingleDrop(dropDownView, this);
        salePriceDrop = new SalePriceDrop(dropDownView, this);
        postRoomDrop = new SingleDrop(dropDownView, this);
        moreDrop = new MoreDrop(dropDownView, this);
        dropDownView.addDrops(singleDrop, salePriceDrop, postRoomDrop, moreDrop);

        final ArrayList<SchoolEstateInfoBo> temp = getIntent().getParcelableArrayListExtra(SchoolToEsfList);
        schoolEstateInfoBos.addAll(temp);
        final ArrayList<DropBo> esfDropBos = new ArrayList<>();
        DropBo dropBo = new DropBo(130);
        dropBo.setValue("");
        dropBo.setText("不限");
        esfDropBos.add(dropBo);
        for (SchoolEstateInfoBo schoolEstateInfoBo : schoolEstateInfoBos) {
            dropBo = new DropBo(130);
            dropBo.setText(schoolEstateInfoBo.getEstateName());
            dropBo.setValue(schoolEstateInfoBo.getEstateCode());
            esfDropBos.add(dropBo);
        }
        singleDrop.init(esfDropBos);

        RxDataBase.getSearchData("Sell")
                .map(new SearchDataFunc1(30))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        salePriceDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Room")
                .map(new SearchDataFunc1(40))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        dropBos.get(0).setSelected(true);
                        postRoomDrop.init(dropBos);
                    }
                });
        Observable.combineLatest(
                RxDataBase.getSearchData("SellOrderby").map(new SearchDataFunc1(100)),
                RxDataBase.getSearchData("Direction").map(new SearchDataFunc1(50)),
                RxDataBase.getSearchData("Area").map(new SearchDataFunc1(60)),
                RxDataBase.getSearchData("Fitment").map(new SearchDataFunc1(70)),
                RxDataBase.getSearchData("SellTag").map(new SearchDataFunc1(80)),
                RxDataBase.getSearchData("Construction").map(new SearchDataFunc1(90)),
                RxDataBase.getSearchData("Property").map(new SearchDataFunc1(110)),
                new Func7<ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>,
                        ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, ArrayList<DropBo> dropBos2,
                                                  ArrayList<DropBo> dropBos3, ArrayList<DropBo> dropBos4,
                                                  ArrayList<DropBo> dropBos5, ArrayList<DropBo> dropBos6,
                                                  ArrayList<DropBo> dropBos7) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo;
                        if (dropBos.size() > 0) {
                            dropBo = new DropBo(100);
                            dropBo.setText("排序");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos);
                            list.add(dropBo);
                        }
                        if (dropBos2.size() > 0) {
                            dropBo = new DropBo(50);
                            dropBo.setText("朝向");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos2);
                            list.add(dropBo);
                        }

                        if (dropBos3.size() > 0) {
                            dropBo = new DropBo(60);
                            dropBo.setText("面积");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos3);
                            list.add(dropBo);
                        }
                        if (dropBos4.size() > 0) {
                            dropBo = new DropBo(70);
                            dropBo.setText("装修");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos4);
                            list.add(dropBo);
                        }
                        if (dropBos5.size() > 0) {
                            dropBo = new DropBo(80);
                            dropBo.setText("特色");
                            dropBo.setSelected(true);
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos5);
                            list.add(dropBo);
                        }
                        if (dropBos6.size() > 0) {
                            dropBo = new DropBo(90);
                            dropBo.setText("房龄");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos6);
                            list.add(dropBo);
                        }
                        if (dropBos7.size() > 0) {
                            dropBo = new DropBo(110);
                            dropBo.setText("类型");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos7);
                            list.add(dropBo);
                        }
                        return list;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        moreDrop.init(dropBos);
                    }
                });
    }
}
